package com.ptszyxx.popose.module.main.home.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.baidu.ocr.sdk.utils.HttpsClient;
import com.ptszyxx.popose.common.event.UserEditEvent;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.module.main.gift.vm.GiftAndCallVM;
import com.ptszyxx.popose.module.main.user.vm.UserDetailVM;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.dynamic.DynamicResult;
import com.ysg.http.data.entity.user.UserResult;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class HomeWorldDetailVM extends GiftAndCallVM {
    public String userId;
    public ObservableField<UserResult> userEntity = new ObservableField<>();

    public HomeWorldDetailVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onTagEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onInfoEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onChatCommand = new BindingCommand(()-> jumpChat());
    public BindingCommand onRightCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpUserEdit(this, userId));

    //TODO 3.异步通知处理=====================
    public Disposable disposableUser;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposableUser = RxBus.getDefault().toObservable(UserEditEvent.class)
                .subscribe(new Consumer<UserEditEvent>() {
                    @Override
                    public void accept(UserEditEvent event){
                        if (event != null){
                            requestUser();
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposableUser);
    }

    //=========================================接口：start===========================================
    /**
     * 请求用户信息
     */
    public void requestUser(){
        Map<String, String> map = new HashMap<>();
        map.put("userid", userId);

        HttpUtils.getInstance()
                .data(model.userInfo(map), this,
                        new OnSuccessResult<UserResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserResult> response) {
                                UserResult result = response.getData();
                                userEntity.set(result);
                                uc.onTagEvent.call();
                                uc.onInfoEvent.call();
                            }
                        });
    }
    /**
     * 动态
     */
    public void requestList(boolean isRefresh){
        if (isRefresh){
            page = 1;
        }else {
            page++;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pageno", String.valueOf(page));
        map.put("userid", String.valueOf(userId));

        HttpUtils.getInstance()
                .list(model.userDynamicPage(map), this,
                        new OnSuccessListResult<DynamicResult>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<DynamicResult> response) {
                                uc.onRefreshEvent.setValue(response.getData());
                            }
                        });
    }


    //=========================================接口：end=============================================
    /**
     * 聊天
     */
    public void jumpChat(){
        if (userEntity.get() != null){
            YActivityUtil.getInstance().jumpChat(this, userEntity.get().getTengxuncode()+"", userEntity.get().getNick(), userEntity.get().getPic());
        }
    }



}
